<?php
/**
 * @author 	Vu Nhat Lam
 * 			Mail: lamvnd@gmail.com
 * 			Mobile: 0984 52 62 68
 * @version 1.0.0
 */
// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################
define('THIS_SCRIPT', 'btupload');

// ########################## REQUIRE BACK-END ############################
require_once('./global.php');

// #######################################################################
// ######################### START MAIN SCRIPT ###########################
// #######################################################################
function select_forum($parentid = -1)
{
	global $vbulletin;

	if (empty($vbulletin->iforumcache))
	{
		// get the vbulletin->iforumcache, as we use it all over the place, not just for forumjump
		cache_ordered_forums(0, 1);
	}

	if (empty($vbulletin->iforumcache["$parentid"]) OR !is_array($vbulletin->iforumcache["$parentid"]))
	{
		return;
	}

	foreach($vbulletin->iforumcache["$parentid"] AS $forumid)
	{
		$forumperms = $vbulletin->userinfo['forumpermissions']["$forumid"];
		if ((!($forumperms & $vbulletin->bf_ugp_forumpermissions['canview']) AND ($vbulletin->forumcache["$forumid"]['showprivate'] == 1 OR (!$vbulletin->forumcache["$forumid"]['showprivate'] AND !$vbulletin->options['showprivateforums']))) OR !($vbulletin->forumcache["$forumid"]['options'] & $vbulletin->bf_misc_forumoptions['showonforumjump']) OR !$vbulletin->forumcache["$forumid"]['displayorder'] OR !($vbulletin->forumcache["$forumid"]['options'] & $vbulletin->bf_misc_forumoptions['active']))
		{
			continue;
		}
		else
		{
			// set $forum from the $vbulletin->forumcache
			$forum = $vbulletin->forumcache["$forumid"];

			$children = explode(',', trim($forum['childlist']));
			if (sizeof($children) <= 2)
			{
				$jumpforumbits[] = array('type' => 'link', 'forum' => $forum);
			}
			else
			{
				 if ($forumbits = select_forum($forumid))
				 {
				 	$forum['depth']++;
				  	$jumpforumbits[] = array('type' => 'subforum_begin', 'forum' => $forum);
				  	$jumpforumbits = array_merge($jumpforumbits, $forumbits);
				  	$jumpforumbits[] = array('type' => 'subforum_end');
				 }
				 else
				 {
				  	$jumpforumbits[] = array('type' => 'link', 'forum' => $forum);
				 }
			}

		} // if can view
	} // end foreach ($vbulletin->iforumcache[$parentid] AS $forumid)

	return $jumpforumbits;
}

// draw nav bar
$navbits = array();
$navbits[fetch_seo_url('forumhome', array())] = $vbphrase['forum'];
$navbits['btuploadnew.php' . $vbulletin->session->vars['sessionrl_q']] = $vbphrase['bt_upload'];
$navbits = construct_navbits($navbits);
$navbar = render_navbar_template($navbits);

$jumpforumbits = select_forum(129);

construct_quick_nav();

$templater = vB_Template::create('btupload');
	$templater->register_page_templates();
	$templater->register('navbar', $navbar);
	$templater->register('jumpforumbits', $jumpforumbits);
	$templater->register('forumjump', $forumjump);

print_output($templater->render());
